﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GroupCheer.DBHelper;
using System.Data.SqlClient;
using System.Data;
using GroupCheer.Common;
using Newtonsoft.Json.Linq;

namespace GroupCheer
{
    public partial class GoalInfo : System.Web.UI.Page
    {
        string GroupId = "256627431097033";
        protected void Page_Load(object sender, EventArgs e)
        {
            this.lblGoalInfo.Text = "Week2:Zaric Rich's Goal(Lose 30 lbs in 3 months)\n"
            + "Current Weight: 205 lbs\n"
            + "Target Weight: 175 lbs\n"
            + "Start Date: 04/01/2012\n"
            + "End Date:06/30/2012\n"
            + "First Checkpoint Date:04/08/2012";
        }

        protected void btnSaveAndPost_Click(object sender, EventArgs e)
        {
            int golaId = SaveGoalToDB();
            string FBid = PostTopicToFB(lblGoalInfo.Text, null, null, null, null, null, null);
            if(!string.IsNullOrEmpty(FBid)) 
                SaveTopicToDB(golaId, FBid, lblGoalInfo.Text, null, null, null);
        }

        protected void btnJustSave_Click(object sender, EventArgs e)
        {

        }

        private int SaveGoalToDB()
        {
            int returnGoalId = 0;
            try
            {
                SqlParameter[] paramsToStore = new SqlParameter[9];

                paramsToStore[0] = new SqlParameter("@Goal_Id", SqlDbType.Int);
                paramsToStore[0].Direction = ParameterDirection.Output;
                paramsToStore[1] = new SqlParameter("@Socal_Media_ID", SqlDbType.Int);
                paramsToStore[1].Value = DBNull.Value;
                paramsToStore[2] = new SqlParameter("@Friends_List_Id", SqlDbType.Int);
                paramsToStore[2].Value = DBNull.Value;
                paramsToStore[3] = new SqlParameter("@Login_Id", SqlDbType.Int);
                paramsToStore[3].Value = DBNull.Value;
                paramsToStore[4] = new SqlParameter("@Topic_Id", SqlDbType.Int);
                paramsToStore[4].Value = DBNull.Value;
                paramsToStore[5] = new SqlParameter("@Start_Date", SqlDbType.DateTime);
                paramsToStore[5].Value = DBNull.Value;
                paramsToStore[6] = new SqlParameter("@Complete_Date", SqlDbType.DateTime);
                paramsToStore[6].Value = DBNull.Value;
                paramsToStore[7] = new SqlParameter("@Check_Point_Date", SqlDbType.NVarChar);
                paramsToStore[7].Value = DBNull.Value;
                paramsToStore[7].Size = 1000;
                paramsToStore[8] = new SqlParameter("@Progress_Viewable", SqlDbType.NChar);
                paramsToStore[8].Value = DBNull.Value;

                using (DBContext db = new DBContext())
                {
                    SqlResult result = db.CallProcedure("usp_goal_add_edit", paramsToStore);
                    returnGoalId = int.Parse(result.OutResult["@Goal_Id"].ToString());
                }
            }
            catch (Exception excp)
            {
                throw excp;
            }
            return returnGoalId;
        }
        private int SaveTopicToDB(int goalId, string FBid, string message, string location, string link, string token)
        {
            int returnTopicId = 0;
            try
            {
                SqlParameter[] paramsToStore = new SqlParameter[7];

                paramsToStore[0] = new SqlParameter("@topic_id", SqlDbType.Int);
                paramsToStore[0].Direction = ParameterDirection.Output;
                paramsToStore[1] = new SqlParameter("@goal_id", SqlDbType.Int);
                paramsToStore[1].Value = goalId;
                paramsToStore[2] = new SqlParameter("@FB_id", SqlDbType.NVarChar);
                paramsToStore[2].Value = FBid;
                paramsToStore[2].Size = 100;
                paramsToStore[3] = new SqlParameter("@topic_message", SqlDbType.NVarChar);
                paramsToStore[3].Value = message;
                paramsToStore[3].Size = 1000;
                paramsToStore[4] = new SqlParameter("@Topic_image_location", SqlDbType.NVarChar);
                paramsToStore[4].Value = location;
                paramsToStore[4].Size = 100;
                paramsToStore[5] = new SqlParameter("@Topic_link", SqlDbType.NVarChar);
                paramsToStore[5].Value = link;
                paramsToStore[5].Size = 100;
                paramsToStore[6] = new SqlParameter("@Topic_Token", SqlDbType.NVarChar);
                paramsToStore[6].Value = token;
                paramsToStore[6].Size = 1000;

                using (DBContext db = new DBContext())
                {
                    SqlResult result = db.CallProcedure("usp_topic_add_edit", paramsToStore);
                    returnTopicId = int.Parse(result.OutResult[paramsToStore[0].ParameterName].ToString());
                }
            }
            catch (Exception excp)
            {
                throw excp;
            }

            return returnTopicId;
        }

        private string PostTopicToFB(string message, string picture, string link, string name, string caption, string description, string source)
        {
            oAuthFacebookApi oAuth = oAuthFacebookApi.Instance;
            oAuth.UserID = GroupId;
            string objValue = oAuth.PostWallMessage(message, picture, link, name, caption, description, source);
            Dictionary<string, string> results = new Dictionary<string, string>();
            JObject jsonObject = JObject.Parse(objValue);

            if (jsonObject.Properties().Count() > 0)
            {
                results.Add("id", (jsonObject.SelectToken("id", false) as JValue).Value.ToString());
            }
            if (!string.IsNullOrEmpty(results["id"]) && results["id"].Contains("_"))
            {
                return results["id"].Substring(results["id"].IndexOf("_") + 1);
            }
            else if (!string.IsNullOrEmpty(results["id"]))
            {
                return results["id"];
            }
            else
            {
                return "";
            }
        }
    }
}
